package com.wy.ifly.iview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:7/24/21
 * Author:ypf
 * Description:
 */
public class FlowLayout extends ViewGroup {


    /**
     * 日志标签
     */
    private String TAG = "FlowLayout";

    /**
     * 上下文
     */
    private Context mContext;

    /**
     * 屏幕宽
     */
    private int mSreenWidth;

    /**
     * 屏幕高
     */
    private int mSreenHeight;

    /**
     * 记录当前行用了多少宽度
     */
    private int mRecordLineWidth = 0;

    /**
     * 记录当前行用了多少宽度
     */
    private List<List<View>> mListView;

    /**
     * 记录行数
     */
    private int mRecordLines = -1;

    //思路
    //1.获取屏幕宽高
    //2.计算每行能放多少控件

    /**
     * new中用到的
     */
    public FlowLayout(Context context) {
        super(context);
        init(context);
    }

    /**
     * xml中用到的
     */
    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 主题中用到的
     */
    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        log("onMeasure");

        //onMeasure方法会执行两次，所以放在这儿初始化
        mListView = new ArrayList<>();
        mRecordLines = -1;
        mRecordLineWidth = 0;

        //获取孩子个数，并计算每个孩子宽高
        //判断一行能发下几个孩子，放不下换行
        //判断高度，并且判断高度还够不够

        //控件宽
        int widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMeasureSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        log("widthMeasureSpecSize:" + widthMeasureSpecSize);
        log("heightMeasureSpecSize:" + heightMeasureSpecSize);

        //获取孩子个数
        int childCount = getChildCount();
        log("childCount:" + childCount);

        //计算每个孩子宽高
        for (int i = 0; i < childCount; i++) {

            //孩子
            View childAt = getChildAt(i);

            //孩子宽高
            LayoutParams childLP = childAt.getLayoutParams();

            //孩子宽高
            int width = childLP.width;
            log("width:" + width);

            //用于临时判断，已使用的宽度+要加入的子视图宽度是否大于总宽度
            int recordLineWidth = width + mRecordLineWidth;

            //此行已满，新建一行
            if (0 == mRecordLineWidth || recordLineWidth > mSreenWidth) {
                //行数+1
                mRecordLines++;
                //第一行时
                ArrayList<View> list = new ArrayList<>();
                list.add(childAt);
                mListView.add(list);

                //累加已经使用的宽度
                mRecordLineWidth = 0;
                mRecordLineWidth = mRecordLineWidth + width;
            } else {
                //此行未满，不用新建一行
                mListView.get(mRecordLines).add(childAt);
                //累加已经使用的宽度
                mRecordLineWidth = mRecordLineWidth + width;
            }

            log("mRecordLineWidth:" + mRecordLineWidth + " ,mSreenWidth:" + mSreenWidth);

        }

        log("listView size:" + mListView.size());

    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

        log("onLayout b:" + b + ",i:" + i + ",i1:" + i1 + ",i2:" + i2 + ",i3:" + i3);

        int l;
        int t = 0;
        int tt = 0;

        for (int i4 = 0; i4 < mListView.size(); i4++) {

            l = 0;

            List<View> list = mListView.get(i4);

            for (int i5 = 0; i5 < list.size(); i5++) {

                View view = list.get(i5);
                LayoutParams layoutParams = view.getLayoutParams();
                view.layout(l, t, l + layoutParams.width, t + layoutParams.height);
                log("l:" + l + ",t:" + t + ",r:" + (l + layoutParams.width) + ",b:" + (t + layoutParams.height));
                l = l + layoutParams.width;
                tt = Math.max(tt, layoutParams.height);

            }

            t = t + tt;

        }

    }

    /**
     * 初始化配置
     */
    private void init(Context context) {
        log("init");
        //获取上下文
        mContext = context;
        //获取屏幕宽高
        getScreenWidthHeight();
    }

    /**
     * 获取屏幕宽高
     */
    private void getScreenWidthHeight() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        mSreenWidth = displayMetrics.widthPixels;
        mSreenHeight = displayMetrics.heightPixels;
        log("mSreenWidth:" + mSreenWidth);
        log("mSreenHeight:" + mSreenHeight);
    }

    /**
     * 打日志
     */
    private void log(String msg) {
        Log.i(TAG, msg);
    }


}
